package com.prone.vyuan.widget.dpdialog;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.dongdongsongcan.app.R;
import com.prone.vyuan.widget.wheel.OnWheelChangedListener;
import com.prone.vyuan.widget.wheel.OnWheelScrollListener;
import com.prone.vyuan.widget.wheel.WheelView;
import com.prone.vyuan.widget.wheel.adapters.ArrayWheelAdapter;
import com.su.dolphin.DpApp;

/**
 * 双滚轮dialog 用于显示限制，两对滚轮显示内容一致
 * 
 * @author Prone
 */
public class DoubleWheelDialog {
	/**
	 * 双滚轮dialog的限制规则样式
	 */
	public static enum WheelDialogStyle {
		/**
		 * 下限不包含边界
		 */
		STYLE_LOWER,
		/**
		 * 下限包含边界
		 */
		STYLE_LOWER_BOUNDARY,

		/**
		 * 下限不包含边界，包含固定选项 固定选项是每次构造元数据默认放入
		 * 所以一级数据和二级数据不同步，二级数据比一级数据少一项，加上二级数据加入的默认选项，则两个列表同步
		 */
		STYLE_LOWER_ANY,

		/**
		 * 下限包含边界,包含固定选项 固定选项是每次构造元数据默认放入
		 * 所以一级数据和二级数据不同步，二级数据比一级数据少一项，加上二级数据加入的默认选项，则两个列表同步
		 */
		STYLE_LOWER_BOUNDARY_ANY,

		/**
		 * 无限制
		 */
		STYLE_NONE
	}

	private Context mContext;
	private String[] mData1;
	private String[] mData2;
	private String[] mData2Swap;
	private String mDEfaultPosition1;
	private String mDefaultPosition2;
	private String mTitle;
	private OnDoubleWheelChangeListener mListener;
	private WheelDialogStyle mStyle = WheelDialogStyle.STYLE_NONE;
	private float mTextSize;
	private int mTextMinHeight;
	private boolean mIsWheel1Scrolling = false;
	private WheelView mWheel1;
	private WheelView mWheel2;
	private String anyString = "";

	/**
	 * 双轮Dialog构造
	 * 
	 * @param ctx
	 *            上下文
	 * @param style
	 *            限制样式
	 * @param title
	 *            标题
	 * @param data1
	 *            数据源1
	 * @param data2
	 *            数据源2
	 * @param defaultpos1
	 *            数据源1默认选中项目
	 * @param defaultpos2
	 *            数据源2默认选中项目
	 * @param listenr
	 *            选中后监听
	 */
	public DoubleWheelDialog(Context ctx, WheelDialogStyle style, String title,
			String[] data1, String[] data2, String defaultpos1,
			String defaultpos2, OnDoubleWheelChangeListener listenr) {
		mContext = ctx;
		mStyle = style;
		mTitle = title;
		this.mData1 = data1;
		this.mData2 = data2;
		mDEfaultPosition1 = defaultpos1;
		mDefaultPosition2 = defaultpos2;
		mListener = listenr;

		mTextSize = ctx.getResources().getDimension(
				R.dimen.dialog_city_textSize);
		mTextMinHeight = ctx.getResources().getDimensionPixelSize(
				R.dimen.dialog_city_textHeight);
	}

	/**
	 * 双轮Dialog构造
	 * 
	 * @param ctx
	 *            上下文
	 * @param style
	 *            限制样式
	 * @param title
	 *            标题
	 * @param data1
	 *            数据源1
	 * @param data2
	 *            数据源2
	 * @param defaultpos1
	 *            数据源1默认选中项目
	 * @param defaultpos2
	 *            数据源2默认选中项目
	 * @param listenr
	 *            选中后监听
	 */
	public DoubleWheelDialog(Context ctx, WheelDialogStyle style, String title,
			String any, String[] data1, String[] data2, String defaultpos1,
			String defaultpos2, OnDoubleWheelChangeListener listenr) {
		mContext = ctx;
		mStyle = style;
		mTitle = title;
		this.mData1 = data1;
		this.mData2 = data2;
		mDEfaultPosition1 = defaultpos1;
		mDefaultPosition2 = defaultpos2;
		mListener = listenr;
		anyString = any;

		mTextSize = ctx.getResources().getDimension(
				R.dimen.dialog_city_textSize);
		mTextMinHeight = ctx.getResources().getDimensionPixelSize(
				R.dimen.dialog_city_textHeight);
	}

	/**
	 * 构造探出对话框
	 * 
	 * @return 对话框构造器
	 */
	public CustomDialog builder() {

		View contentView = LayoutInflater.from(DpApp.getContext()).inflate(
				R.layout.dpdialog_location_wheel, null);
		contentView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		mWheel1 = (WheelView) contentView.findViewById(R.id.wheel1);
		mWheel2 = (WheelView) contentView.findViewById(R.id.wheel2);

		ArrayWheelAdapter<String> wheel1Adapter = new ArrayWheelAdapter<String>(
				mContext, mData1);
		wheel1Adapter.setTextSize(mTextSize);
		wheel1Adapter.setTextMinHeight(mTextMinHeight);
		mWheel1.setViewAdapter(wheel1Adapter);

		int w1Position = 0;
		if (!TextUtils.isEmpty(mDEfaultPosition1)) {
			for (int i = 0; i < mData1.length; i++) {
				if (mDEfaultPosition1.equals(mData1[i])) {
					w1Position = i;
					break;
				}
			}
		}

		mWheel1.setCurrentItem(w1Position);

		setWheel2(mWheel2, mDefaultPosition2);
		mWheel1.addScrollingListener(mWheel1ScrollListener);
		mWheel1.addChangingListener(mWheel1ChangeListener);

		return DialogTool.buildCustomDialog(mContext, 0, mTitle, contentView,
				"是", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (mListener != null) {
							// int wheelSelectedPosition = 0;
							// String wheelSelectText =
							// mData2Swap[mWheel2.getCurrentItem()];
							// int swapLen = mData2.length;
							// for (int i = 0; i < swapLen; i++) {
							// if (wheelSelectText.equals(mData2[i])) {
							// wheelSelectedPosition = i;
							// break;
							// }
							// }
							mListener.onDoubleWheelChangeListener(
									mData1[mWheel1.getCurrentItem()],
									mData2Swap[mWheel2.getCurrentItem()]);
						}
					}
				}, null, null);
	}

	private OnWheelScrollListener mWheel1ScrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(WheelView wheel) {
			mIsWheel1Scrolling = true;
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			setWheel2(mWheel2, null);
		}

	};

	private OnWheelChangedListener mWheel1ChangeListener = new OnWheelChangedListener() {

		@Override
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			if (!mIsWheel1Scrolling) {
				setWheel2(mWheel2, null);
			}
		}

	};

	private void setWheel2(WheelView wheel2, String defualutPosition) {
		if (mStyle == WheelDialogStyle.STYLE_NONE) {
			if (mData2Swap == null) {
				mData2Swap = mData2;
			} else {
				return;
			}
		} else if (mStyle == WheelDialogStyle.STYLE_LOWER) {
			int swaplen = mData2.length - mWheel1.getCurrentItem() - 1;
			mData2Swap = new String[swaplen];
			System.arraycopy(mData2, mWheel1.getCurrentItem() + 1, mData2Swap,
					0, swaplen);
		} else if (mStyle == WheelDialogStyle.STYLE_LOWER_BOUNDARY) {
			int swaplen = mData2.length - mWheel1.getCurrentItem();
			mData2Swap = new String[swaplen];
			System.arraycopy(mData2, mWheel1.getCurrentItem(), mData2Swap, 0,
					swaplen);
		} else if (mStyle == WheelDialogStyle.STYLE_LOWER_ANY) {
			int srcPosition = mWheel1.getCurrentItem();
			// 判断末尾界限
			srcPosition = srcPosition >= mData2.length ? mData2.length
					: srcPosition;
			int swaplen = mData2.length - srcPosition + 1;

			mData2Swap = new String[swaplen];
			mData2Swap[0] = anyString;

			System.arraycopy(mData2, srcPosition, mData2Swap, 1, swaplen - 1);
		} else if (mStyle == WheelDialogStyle.STYLE_LOWER_BOUNDARY_ANY) {
			int srcPosition = mWheel1.getCurrentItem() - 1;
			// 判断开始界限
			srcPosition = srcPosition < 0 ? 0 : srcPosition;
			int swaplen = mData2.length - srcPosition + 1;
			mData2Swap = new String[swaplen];
			mData2Swap[0] = anyString;

			System.arraycopy(mData2, srcPosition, mData2Swap, 1, swaplen - 1);
		} else {
			return;
		}

		ArrayWheelAdapter<String> wheel2Adapter = new ArrayWheelAdapter<String>(
				mContext, mData2Swap);
		wheel2Adapter.setTextSize(mTextSize);
		wheel2Adapter.setTextMinHeight(mTextMinHeight);
		wheel2.setViewAdapter(wheel2Adapter);

		int w2Position = 0;
		if (!TextUtils.isEmpty(defualutPosition)) {
			for (int i = 0; i < mData2Swap.length; i++) {
				if (defualutPosition.equals(mData2Swap[i])) {
					w2Position = i;
					break;
				}
			}
		}

		if (mData2Swap.length > 0) {
			wheel2.setCurrentItem(w2Position);
		}

	}

	/**
	 * 双轮Dialog选中
	 * 
	 * @author Prone
	 */
	public static interface OnDoubleWheelChangeListener {
		void onDoubleWheelChangeListener(String text1, String text2);
	}
}
